
package com.ruoyi.group.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.group.domain.BusGoods;
import com.ruoyi.group.domain.CustomerVisit;
import com.ruoyi.group.domain.GoodsCategory;
import com.ruoyi.group.domain.vo.GoodsCategoryVo;
import com.ruoyi.group.domain.vo.GoodsTreeVo;
import com.ruoyi.group.mapper.BusGoodsMapper;
import com.ruoyi.group.mapper.CustomerVisitMapper;
import com.ruoyi.group.mapper.GoodsCategoryMapper;
import com.ruoyi.group.service.IBusGoodsService;
import com.ruoyi.group.service.IGoodsCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.*;

@Service
public class GoodsCategoryServiceImpl extends ServiceImpl<GoodsCategoryMapper, GoodsCategory>  implements IGoodsCategoryService {
    @Resource
    private GoodsCategoryMapper mapper;
    @Resource
    private BusGoodsMapper busGoodsMapper;
    /**
     * 查询物品分类信息列表
     *
     * @param goodsCategory 物品分类信息
     * @return 物品分类信息
     */
    @Override
    public List<GoodsCategory> selectGoodsCategoryList(GoodsCategory goodsCategory) {
        if (!StringUtils.isEmpty(goodsCategory.getBusiPath())){
            GoodsCategory category2 = super.getById(goodsCategory.getBusiPath());
            goodsCategory.setBusiPath(category2.getBusiPath());
        }
        List<GoodsCategory> goodsCategories = getBaseMapper().selectGoodsCategoryList(goodsCategory);
        for (GoodsCategory category : goodsCategories) {
            if (category.getParentId()!=null){
                Long parentId = category.getParentId();
                GoodsCategory category1 = super.getById(parentId);
                category.setParentName(category1.getCategoryName());
            }
        }
        return goodsCategories;
    }

    @Override
    public List<GoodsTreeVo> selectList(GoodsCategoryVo goodsCategory) {
        List<GoodsCategoryVo> goodsCategories = mapper.selectList(goodsCategory);
        HashMap<Long, GoodsTreeVo> map = new HashMap<>();
        List<GoodsTreeVo> goodsTreeVos = new ArrayList<>();
        for (GoodsCategoryVo category : goodsCategories) {
            GoodsTreeVo goodsTreeVo = new GoodsTreeVo(category.getId(), category.getCategoryName());
            map.put(category.getId(),goodsTreeVo);
        }
        Iterator<GoodsCategoryVo> iterator = goodsCategories.iterator();
        while (iterator.hasNext()){
            GoodsCategory category = iterator.next();
            Long parentId = category.getParentId();
            if (parentId!=null){
                GoodsTreeVo parent = map.get(parentId);
                if (parent!=null){
                    parent.getChildren().add(map.get(category.getId()));
                    iterator.remove();
                }
            }else {
                GoodsTreeVo parent = map.get(category.getId());
                goodsTreeVos.add(parent);
            }
        }
        return goodsTreeVos;
    }

    @Override
    public int removeGoodsByIds(String asList) {

        return getBaseMapper().removeGoodsByIds(asList);
    }

    @Override
    public int saveGood(GoodsCategory goodsCategory) {

        String substring = UUID.randomUUID().toString().replaceAll("-", "").substring(1, 5);
        Long parentId = goodsCategory.getParentId();
        if (parentId!=null){
            GoodsCategory category = super.getById(parentId);
            goodsCategory.setBusiPath(category.getBusiPath()+":"+substring);
        }else {
            goodsCategory.setBusiPath(substring.substring(1));
        }

        return getBaseMapper().insert(goodsCategory);
    }
//包括子类
    @Override
    public void removeGoods(Long id, Long parentId) {
        List<BusGoods> busGoods = busGoodsMapper.selectList(new LambdaQueryWrapper<BusGoods>().eq(BusGoods::getCategoryId, id));
        if (CollectionUtils.isEmpty(busGoods)){
            throw new ServiceException("该分类中的物品为空");
        }
        List<Long> ids = new ArrayList<>();
        GoodsCategory goodsCategory = super.getById(id);
        String busiPath = goodsCategory.getBusiPath();
        List<GoodsCategory> categories = getBaseMapper().selectBybusiPath(busiPath);
        for (GoodsCategory category : categories) {
            ids.add(category.getId());
        }
        getBaseMapper().removegood(ids,parentId);
    }
     //不包括子类
    @Override
    public void removeGood(Long id, Long parentId) {
        List<BusGoods> busGoods = busGoodsMapper.selectList(new LambdaQueryWrapper<BusGoods>().eq(BusGoods::getCategoryId, id));
        if (CollectionUtils.isEmpty(busGoods)){
            throw new ServiceException("该分类中的物品为空");
        }
        getBaseMapper().updategoods(id,parentId);
    }
}
